package com.example.final_project.Home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.example.final_project.Home.entry.CarNews;
import com.example.final_project.R;
import com.example.final_project.Usr.entry.GuildSrc;

import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<CarNews> news;
    public static final int BANNER = 0;
    public static final int GUILD = 1;
    public static final int CARNEWS = 2;

    public HomeAdapter(Context context,List<CarNews> news){
        this.context = context;
        ArrayList<CarNews> cars = new ArrayList<CarNews>();
        cars.addAll(news);
        this.news = cars;
    }

    public void setData(List<CarNews> cars){
        news.addAll(cars);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case BANNER:
                View banner = LayoutInflater.from(context).inflate(R.layout.home_car_banner,parent,false);
                holder = new BannerHolder(banner);
                break;
            case GUILD:
                View guild = LayoutInflater.from(context).inflate(R.layout.home_car_guild,parent,false);
                holder = new GuildHolder(guild);
                break;
            case CARNEWS:
                View car = LayoutInflater.from(context).inflate(R.layout.home_car,parent,false);
                holder = new CarHolder(car);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            int type = getItemViewType(position);
            switch (type){
                case BANNER:
                    BannerHolder  bannerHolder = (BannerHolder) holder;
                    bannerHolder.setBanner();
                    break;
                case GUILD:
                    GuildHolder guildHolder = (GuildHolder) holder;
                    guildHolder.setGuild();
                    break;
                case CARNEWS:
                    CarHolder carHolder = (CarHolder) holder;
                    carHolder.setNews();
                    break;
            }
    }

    @Override
    public int getItemViewType(int position) {
        int type = 0;
        switch (position){
            case BANNER:
                type=BANNER;
                break;
            case GUILD:
                type=GUILD;
                break;
            case CARNEWS:
                type=CARNEWS;
                break;
        }
        return type;
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    //汽车新闻轮播图
    class BannerHolder extends RecyclerView.ViewHolder {
        public BannerHolder(@NonNull View itemView) {
            super(itemView);

        }

        public void setBanner(){
            //找到控件
            SliderLayout banner = (SliderLayout)itemView.findViewById(R.id.banner_SL);
            //设置轮播图照片
            TextSliderView textSliderView1 = new TextSliderView(context);
            textSliderView1.description("世纪长安，创领未来").image("http://10.150.11.9:8086/chengcar/banners/banner4.jpeg");
            banner.addSlider(textSliderView1);

            TextSliderView textSliderView2 = new TextSliderView(context);
            textSliderView2.description("丰田LC76高配").image("http://10.150.11.9:8086/chengcar/banners/banner1.jpeg");
            banner.addSlider(textSliderView2);

            TextSliderView textSliderView3 = new TextSliderView(context);
            textSliderView3.description("Bamma丝圈系列").image("http://10.150.11.9:8086/chengcar/banners/banner2.jpeg");
            banner.addSlider(textSliderView3);

            TextSliderView textSliderView4 = new TextSliderView(context);
            textSliderView4.description("天猫聚划算，全场地址9.9元").image("http://10.150.11.9:8086/chengcar/banners/banner3.jpeg");
            banner.addSlider(textSliderView4);

            //设置轮播图停留时间
            banner.setDuration(2000);
            //设置轮播图切换动画
            banner.setPresetTransformer(SliderLayout.Transformer.Accordion);
            //设置轮播图下方按钮位置
            banner.setPresetIndicator(SliderLayout.PresetIndicators.Right_Bottom);
        }
    }

    //导航
    class GuildHolder extends RecyclerView.ViewHolder {
        public GuildHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setGuild(){
            List<GuildSrc> guilds = initData();
            RecyclerView guildRecycle = itemView.findViewById(R.id.guild_RV);
            GuildAdapter guildAdapter = new GuildAdapter(context,guilds);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,5);
            guildRecycle.setAdapter(guildAdapter);
            guildRecycle.setLayoutManager(gridLayoutManager);
        }

        public List<GuildSrc> initData(){

            List<GuildSrc> guilds = new ArrayList<>();

            GuildSrc guildSrc1 = new GuildSrc(R.mipmap.carcate,"车型大全");
            GuildSrc guildSrc2 = new GuildSrc(R.mipmap.paihangbang,"排行榜");
            GuildSrc guildSrc3 = new GuildSrc(R.mipmap.eleccar,"新能源精选");
            GuildSrc guildSrc4 = new GuildSrc(R.mipmap.zhibo,"大神精选车");
            GuildSrc guildSrc5 = new GuildSrc(R.mipmap.maiche,"高价卖车");
            GuildSrc guildSrc6 = new GuildSrc(R.mipmap.chezhufuwu,"车主服务");
            GuildSrc guildSrc7 = new GuildSrc(R.mipmap.qichequanzi,"车友圈");
            GuildSrc guildSrc8 = new GuildSrc(R.mipmap.weizhang,"违章");
            GuildSrc guildSrc9 = new GuildSrc(R.mipmap.remen,"热门车型");
            GuildSrc guildSrc10 = new GuildSrc(R.mipmap.chaxun,"条件选车");

            guilds.add(guildSrc1);
            guilds.add(guildSrc2);
            guilds.add(guildSrc3);
            guilds.add(guildSrc4);
            guilds.add(guildSrc5);
            guilds.add(guildSrc6);
            guilds.add(guildSrc7);
            guilds.add(guildSrc8);
            guilds.add(guildSrc9);
            guilds.add(guildSrc10);

            return guilds;
        }
    }

    //汽车新闻
    class CarHolder extends RecyclerView.ViewHolder {
        public CarHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setNews(){
            RecyclerView carNews = itemView.findViewById(R.id.car_RV);
            CarNewsAdapter carNewsAdapter = new CarNewsAdapter(context,news);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,1);
            carNews.setAdapter(carNewsAdapter);
            carNews.setLayoutManager(gridLayoutManager);
        }

    }

}
